package com.iron.servicemicroironblog.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * 判断枚举
 *
 * @author Aberdeen
 * @date 2024/6/24
 * @apiNote
 */
@AllArgsConstructor
public enum YesOrNoEnum {

    /**
     * 未知
     */
    UNKNOWN(-1, null, "未知", "NONE"),

    /**
     * 否
     */
    NO(0, false,"否", "NO"),

    /**
     * 是
     */
    YES(1, true,"是", "YES"),

    ;

    /**
     * 枚举编号
     */
    @Getter
    private final Integer id;
    /**
     * 枚举值
     */
    @Getter
    private final Boolean value;
    /**
     * 枚举名称
     */
    @Getter
    private final String typeName;
    /**
     * 枚举名
     */
    @Getter
    private final String name;

    /**
     * 根据业务名称（英文）获取枚举
     *
     * @param name 枚举名
     * @return 枚举
     */
    public static YesOrNoEnum fromTypeName(String name) {
        Stream<YesOrNoEnum> stream = Arrays.stream(YesOrNoEnum.values())
                .filter(valueType -> valueType.getName().equals(name));
        Optional<YesOrNoEnum> first = stream.findFirst();
        return first.orElse(UNKNOWN);
    }

    /**
     * 根据id获取枚举值
     *
     * @param id 枚举值
     * @return 枚举
     */
    public static YesOrNoEnum fromId(Integer id) {
        for(YesOrNoEnum yesOrNoEnum : values()) {
            if(Objects.equals(yesOrNoEnum.getId(), id)){
                return yesOrNoEnum;
            }
        }
        return UNKNOWN;
    }
}
